<?php
header('Content-type:text/html;charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<link href="Bingz.css" rel="stylesheet" type="text/css" />
<meta name="URL" content="" />
<title>槟榔2 - wap建站系统</title>
</head>
<body>
<div class="body">
<?php
$mod=$_GET['mod'];
if(file_exists("../config/install.lock")){
echo <<<end
<div class="forum">你现在不能进行安装</div><div class="forum_a">如果你是网站管理员，请手动删除config文件夹下的install.lock文件。如果你不是网站管理员，请自觉离开！<br />
</div>
end;
echo"</div>\n</body>\n</html>";
exit;
}
if($mod == ''){
echo <<<end
<br/><div class="license"><h1>使用提示</h1></div><br/>
<p>
感谢您选择槟榔产品。希望我们的努力能为您提供一个方便.快速.创新.个性化、强大的wap站点提供服务。槟榔官方网址为 http://www.k7t.cn
</p>
<br/><a href="?mod=1"><font color="#FF0000"><b>安装下一步</b></font> </a>
end;
}
if($mod == '1'){
echo <<<end
<br/><div class="license"><h1>检查安装环境</h1></div><br/>
end;
if(phpversion() >= '5.2') {
	  echo"php版本：".phpversion()." <font color='#ff0000'>支持</font><br/>";
}else{
		echo"php版本：".phpversion()." <font color='#ff0000'>不支持</font><br/>";
}
if(mysql_get_client_info() >= '5.0') {
	  echo"mysql数据库：".mysql_get_client_info()." <font color='#ff0000'>支持</font><br/>";
}else{
		echo"mysql数据库：".mysql_get_client_info()." <font color='#ff0000'>不支持</font><br/>";
}
echo <<<end
<br/><div class="license"><h1>检查函数</h1></div><br/>
end;

if(function_exists("mysql_connect")){	
	  echo"mysql_connect()：支持<br/>";
}else{
		echo"mysql_connect()：不支持<br/>";
}
if(function_exists("curl_init")){	
	  echo"curl_init()：支持<br/>";
}else{
		echo"curl_init()：不支持<br/>";
}
if(function_exists("file_get_contents")){	
	  echo"file_get_contents()：支持<br/>";
}else{
		echo"file_get_contents()：不支持<br/>";
}

echo <<<end
<br/><a href="?mod=2"><font color="#FF0000"><b>安装下一步</b></font> </a><br/>
<br/><a href="?mod="><font color="#FF0000"><b>安装上一步</b></font> </a>
end;
}
if($mod == '2'){
	$url_namea=$_SERVER['REQUEST_URI'];
	$url_name =explode("/install/",$url_namea);
  if(!$url_name[0]){
    $url_name ='/';
  }
function getServerName(){
  $ServerName = strtolower($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:$_SERVER['HTTP_HOST']);
  if( strpos($ServerName,'http://')){  
   return str_replace('http://','',$ServerName);
  } 
  return $ServerName;
 }
 $uuu = getServerName();
 function yzmsa($len) {
    $srcstr="qwertyuiopasdfghjklmnbvcxz"; 
    $strs="";
    $i='';
	  for($i=0;$i< $len;$i++){ 
	    $strs.=$srcstr[mt_rand(0,26)]; 
	  }
  return $strs; 
 }
 $sjkmm='bz'.yzmsa('4');
echo <<<end
<form  name="db" action="index.php?mod=3" method="post">
<br/><div class="license"><h1>数据库</h1></div><br/>
    数据库服务器：<input type="text" size=20 name="hostname" value="localhost"/><br/>
    数据库用户名：<input type="text" size=20 name="logname"  value="root"/><br/>
    数据库密  码：<input type="text" size=20 name="dbpwd"    value=""/><br/>
    数据库名    ：<input type="text" size=20 name="dbname"   value="bz"/><br/>
    数据表前缀  ：<input type="text" size=20 name="dbqh"   value="bz_"/><br/>
<br/><div class="license"><h1>管理员初始密码</h1></div><br/>
    用户名：<input type="text" size=20 name="zza" value="admin"/><br/>
    密    码：<input type="text" size=20 name="zzb" value="123456"/><br/><br/>
    <input type="submit" name="sbmt" value="确定安装"/>
</form><br/>
<br/><a href="?mod=1"><font color="#FF0000"><b>安装上一步</b></font> </a>
end;
}
if($mod == '3'){
$hostname=$_POST['hostname'];
$username=$_POST['logname'];
$password=$_POST['dbpwd'];
$dbname  =$_POST['dbname'];
$dbqh    =$_POST['dbqh'];

$zza=$_POST['zza'];
$zzb=$_POST['zzb'];

	if(@$conn = mysql_connect($hostname,$username,$password)){
		if(mysql_select_db($dbname)){
			$modok='4';
		}else{
	    if(mysql_query("CREATE DATABASE ".$dbname."")){
	    	 mysql_select_db($dbname);
			   $modok='4';
	    }else{
		     echo"数据库服务器连接错误，请确认后填写<br/>";
	    }
		}
	}else{
		echo"<b>数据库服务器连接错误，请确认后填写</b><br/>";
	}
}
if($modok=='4'){
mysql_query("set names utf8");
echo <<<end
<br/><div class="license"><h1>网站安装成功</h1></div><br/>
end;
function read_file($filename,$method="rb"){
     if($handle=@fopen($filename,$method)){
         @flock($handle,LOCK_SH);
         $filedata=@fread($handle,@filesize($filename));
         @fclose($handle);
     }
return $filedata;
}
function hfsjk_sql($file,$replace=''){
	  	global $dbqh;
       if($replace == '1'){
       	    $readfiles=$file;
       }else{
       	    $readfiles=read_file($file);
       }
       if($readfiles){
          $readfiles=str_replace('bz_',$dbqh,$readfiles);
       }
       $detail=explode("\n",$readfiles);
       $count=count($detail);
       for($j=0;$j < $count;$j++){
           $ck=substr($detail[$j],0,4);
           if( ereg("#",$ck)||ereg("--",$ck) ){
           continue;
       }
       $array[]=$detail[$j];
       }
       $read=implode("\n",$array); 
       $sql=str_replace("\r",'',$read);
       $detail=explode(";\n",$sql);
       $count=count($detail);
       $s='0';
       $f='0';
       $check='0';
       for($i=0;$i< $count;$i++){
           $sql=str_replace("\r",'',$detail[$i]);
           $sql=str_replace("\n",'',$sql);
           $sql=trim($sql);
           if($sql){
	               if(mysql_query($sql)){
		                $s++;
		             }else{
		             	  echo $sql.'Error=('.mysql_error().')';
		       	        $f++;
			           }
                 $check++;
           }
       }
      return "数据库执行：<font color='#FF0000'>$check</font>条命令 。成功<font color='#FF0000'>$s</font>条，失败<font color='#FF0000'>$f</font>条。 ";
}
echo hfsjk_sql('Bingz.sql');
$confilename = '../config/conn.php';
$config_txt=file_get_contents($confilename);
$config_txt=str_replace('$_config[\'mysql\'][\'host\']=\'localhost\';','$_config[\'mysql\'][\'host\']=\''.$hostname.'\';',$config_txt);
$config_txt=str_replace('$_config[\'mysql\'][\'user\']=\'root\';','$_config[\'mysql\'][\'user\']=\''.$username.'\';',$config_txt);
$config_txt=str_replace('$_config[\'mysql\'][\'pass\']=\'\';','$_config[\'mysql\'][\'pass\']=\''.$password.'\';',$config_txt);
$config_txt=str_replace('$_config[\'mysql\'][\'database\']=\'k7t\';','$_config[\'mysql\'][\'database\']=\''.$dbname.'\';',$config_txt);
$config_txt=str_replace('$_config[\'mysql\'][\'qianh\']=\'bz_\';','$_config[\'mysql\'][\'qianh\']=\''.$dbqh.'\';',$config_txt);

    if (!$handle = fopen($confilename, 'w')) {
         echo "不能打开文件 $filename";
         exit;
    }
    if (fwrite($handle, $config_txt) === FALSE) {
        echo "不能写入到文件 $filename";
        exit;
    }
    fclose($handle);

$fp=fopen("../config/install.lock","w");
flock($fp,LOCK_EX);
$write=fputs($fp,"安装成功！");
flock($fp,LOCK_UN);
fclose($fp);


function user_mm($mm){
	$mma=md5(md5($mm)); 
  $mmb=substr($mma,-18,18);
	$mmc=md5(md5($mmb)); 
  $mmd=substr($mmc,-10,10);
	return $mmd;	
}
$pass=user_mm($zzb);
mysql_query("UPDATE `".$dbqh."user` SET `name` ='$zza' , `pass` ='$pass' WHERE `uid` ='1000'");
echo <<<end
<br/><br/>
<a href="../index.php?"><font color="#FF0000"><b>进入网站首页</b></font> </a>
<br/>
end;
@unlink('index.php');
@unlink('Bingz.sql');
@unlink('Bingz.css');
@rmdir('../install');
}
?>
</div>
</body>
</html>